<?php
namespace Admin\Controller;

class ContentController extends CommonController {
    private $articles;
    private $categories;
    public function __construct()
    {
        parent::__construct();
        $this->articles = M('article');
        $this->categories = M('category');
    }

    public function article_list()
    {
        $Article=D('Article');
        $count      = $Article->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)

        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');

        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Article->relation(true)->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
//        $articles=$Article->relation(true)->order('time desc')->select();
//        $this->assign('articles',$articles);
        $this->display();
    }

    public function category_list()
    {
        $Category=D('Category');
        if(!S('category')){
        $categories=$Category->relation(true)->order('sort_order')->where('parent_id=0')->select();
        S('category',$categories,10);}
        $categories=S('category');
//        $categories=$this->categories->where('parent_id=0')->order('sort_order')->select();
//        foreach ($categories as $key => $value){
//            $id=$value['id'];
//            $children=$this->categories->where("parent_id='$id'")->order('sort_order')->select();
//            $categories[$key]['children']=$children;
//        }
        $this->assign('categories',$categories);
        $this->display();
    }

    public function create_article()
    {
        $categories=$this->categories->where('parent_id=0')->order('sort_order')->select();
        foreach ($categories as $key => $value){
            $id=$value['id'];
            $children=$this->categories->where("parent_id='$id'")->order('sort_order')->select();
            $categories[$key]['children']=$children;
        }
        $this->assign('categories',$categories);
        $this->display();
    }

    function store_article()
    {
        $this->articles->create();
        $this->articles->time=time();
        $this->articles->add();
        $this->success('添加成功',U('article_list'));
    }

    public function edit_article()
    {
        $id=I('get.id');
        $this->assign('article',$this->articles->find($id));
        $this->assign('categories',$this->categories->select());
        $this->display();
    }

    function update_article(){
        $this->articles->create();
        $this->articles->time=time();
        $this->articles->save();
        $this->success('编辑成功',U('article_list'));
    }

    function destroy_article(){
        $id=I('get.id');
        $this->articles->delete($id);
        $this->success('删除成功');
    }

    function destroy_checked(){
        $delete_id=I('post.delete_id');
        foreach ($delete_id as $id){
            $this->articles->delete($id);
        }
        $this->success('删除成功');
    }

    function create_category()
    {
        $categories=$this->categories->where('parent_id=0')->order('sort_order')->select();
        $this->assign('categories',$categories);
        $this->display();
    }

    function destroy_category(){
        $id=I('get.id');
        $this->categories->delete($id);
        $this->success('删除成功');
    }

    function store_category(){
        $this->categories->create();
        $this->categories->add();
        $this->success('添加成功',U('category_list'));
    }

    function sort_order(){
        S('category',NULL);
        $sort_order = I('post.sort_order');
        $id = I('post.id');
        foreach ($id as $key => $value){
            $this->categories->where("id=$value")->setField('sort_order',$sort_order[$key]);

        }
        $this->redirect('category_list');
    }

    public function edit_category()
    {
        $id=I('get.id');
        $Category=D('Category');
        $categories=$Category->relation(true)->order('sort_order')->where('parent_id=0')->select();
        $this->assign('categories',$categories);
        $this->assign('category',$this->categories->find($id));
        $this->display();
    }

    function update_category(){
        $this->categories->create();
        $this->categories->save();
        $this->success('编辑成功',U('category_list'));
    }
}